package org.mags.gps.testing;

import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.fluent.Form;
import org.apache.http.client.fluent.Response;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Assert;
import org.junit.Test;

public class MobileTesting extends HTTPAccess {
	private final String TESTIMEI = "1500000000000000";
	//private final String TESTIMEI = "355354052925569";
	public MobileTesting() {
	}
	public static final void sleep(long milliseconds) {
		try {
			Thread.sleep(milliseconds);
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	@Test
	public void testDoVisited() {
		try {
			connect();
			Form form = Form.form()
					.add("id", number2String(47505));
			Response response = executePost("/service/mobileAccess/dovisited", form);
			String result = response.returnResponse().getStatusLine().toString();
			Assert.assertEquals("HTTP/1.1 200 OK", result);
			disconnect();
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
	@Test
	public void testGetCloseGeopos() {
		try {
			connect();
			Form form = Form.form()
					.add("imei", TESTIMEI)
					.add("latitude", number2String(1.254)) //1 //0.0001 = 11.11 metros   14.688166 
					.add("longitude", number2String(2.4444)) //2    -90.400199    
					.add("altitude", number2String(12));
			Response response = executePost("/service/mobileAccess/closegeopos", form);
			String json = response.returnContent().asString();
			ObjectMapper mapper = new ObjectMapper();
			ArrayList<GeopositionInfo> list  = mapper.readValue(json, new TypeReference<ArrayList<GeopositionInfo>>(){});
			for(GeopositionInfo geo: list) {
				System.out.println(geo);
				System.out.println("\n");
			}
			//String result = response.returnResponse().getStatusLine().toString();
			//Assert.assertEquals("HTTP/1.1 200 OK", result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	@Test
	public void testDistance() { //61 metros
		sendLocation(15.45931453, -90.38193463, 1314, TESTIMEI);
		sleep(1000);
		sendLocation(15.45900183, -90.38148134, 1336, TESTIMEI);
	}
	@Test
	public void testMultiplePostLocation() {
		sendLocation(23.7902, 11.9, 9, TESTIMEI);
		sleep(1000);
		sendLocation(23.8902, 11.9, 10, TESTIMEI);
		sleep(1000);
		sendLocation(23.9902, 11.9, 11, TESTIMEI);
		sleep(1000);
		sendLocation(24.0902, 11.9, 12, TESTIMEI);
		sleep(1000);
		sendLocation(24.1902, 11.9, 13, TESTIMEI);
		sleep(1000);
		sendLocation(24.2902, 11.9, 14, TESTIMEI);
	}
	@Test
	public void testPostLocation() {
		sendLocation(22.9902, 11.9, 4, TESTIMEI);
	}
	protected void sendLocation(double latitude
			, double longitude, double altitude, String imei) {
		try {
			connect();
			Form form = Form.form()
					.add("imei", imei)
					.add("model", "Young")
					.add("mark", "Samsung")
					.add("maker", "Samsung") //23.9902, 11.9
					.add("mobileType", "PHONE") /*PHONE,LAPTOP,TABLET, MODEM     el predeterminado: OTHER*/
					.add("latitude", number2String(latitude)) //1 //0.0001 = 11.11 metros 
					.add("longitude", number2String(longitude)) //2
					.add("altitude", number2String(altitude))
					.add("course", "0")
					.add("speed", "1")
					.add("timestamp", date2String(new Date())) /*FORMAT yyyyMMddHHmmss*/
					.add("comment", "This is a test!");
			Response response = executePost("/service/mobileAccess/location", form);
			String result = response.returnResponse().getStatusLine().toString();
			Assert.assertEquals("HTTP/1.1 200 OK", result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	@Test
	public void testSession() {
		try {
			connect();
			Response response = getPostMethod("/service/sessiontest")
			.execute();
			String result = response.returnResponse().getStatusLine().toString();
			Assert.assertEquals("HTTP/1.1 200 OK", result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	@Test
	public void testPostMessage() {
		try {
			connect();
			Form form = Form.form()
					.add("imei", TESTIMEI)
					.add("model", "Young")
					.add("mark", "Samsung")
					.add("maker", "Samsung")
					.add("mobileType", "PHONE") /*PHONE,LAPTOP,TABLET, MODEM     el predeterminado: OTHER*/
					.add("subject", "Apagando dispositivo...")  
					.add("content", "Apagando dispotitivo por actualización de software")
					.add("messageType", "AUTOMATIC") //OPCIONAL puede ser AUTOMATIC,CRITICAL,WARNING,OTHER, predeterminado: OTHER
					.add("timestamp", date2String(new Date())) /*FORMAT yyyyMMddHHmmss*/
					.add("comment", "This is a test!");
			Response response = executePost("/service/mobileAccess/message", form);
			String result = response.returnResponse().getStatusLine().toString();
			Assert.assertEquals("HTTP/1.1 200 OK", result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	@Test
	public void testPostGeoposition() {
		try {
			connect();
			Form form = Form.form()
				.add("geoposname", "trece cero cero")
				.add("geoposcode", "130000000000") 
				.add("refcode", "130000000000")
				.add("extrainfo", "extrainformation")
				.add("routecode", "021")
				.add("geopostype", "CUST") //PUEDE SER CUST = cliente, SUP = proveedor, OTHER = otro, EMP = empleado, PHYSLOC = Localidad física
				.add("latitude", "24.9") 
				.add("longitude", "10.4")
				.add("altitude", "12")
				.add("comment", "Esta es una gran prueba!");
			Response response = executePost("/service/mobileAccess/geoposition", form);
			String result = response.returnResponse().getStatusLine().toString();
			Assert.assertEquals("HTTP/1.1 200 OK", result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	@Test
	public void testGetFile() {
		try {
			connect();
			Response response = executeGet("/service/mobileAccess/file?mfcode=ZOOM_CAMERA", null);
			byte[] c = IOUtils.toByteArray(response.returnContent().asStream());
			System.out.println(new String(c));
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	@Test
	public void testGetUpdates() {
		try {
			connect();
			Response response = executeGet("/service/mobileAccess/updates", null);
			System.out.println(response.returnContent().asString());
			//String result = response.returnResponse().getStatusLine().toString();
			//System.out.println(result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	
	@Test
	public void testGetStringParam() {
		try {
			connect();
			Response response = executeGet("/service/mobileAccess/strparam?paramcode=TEST_PARAM", null);
			System.out.println(response.returnContent().asString());
			//String result = response.returnResponse().getStatusLine().toString();
			//System.out.println(result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
	@Test
	public void testGetNewVersion() {
		try {
			connect();
			Response response = executeGet("/service/mobileAccess/newversion?mfversion=10&mfcode=ZOOM_CAMERA", null);
			System.out.println(response.returnContent().asString());
			//String result = response.returnResponse().getStatusLine().toString();
			//System.out.println(result);
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			disconnect();
		}
	}
}
